import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.*;

/**
 * @Author: fei.wu
 * @Email: fei.wu@rograndec.com
 * @CreateDate: 2018/8/1
 * @Version: 1.0
 */
public class FileCopy {

    public static String dirPath = "/Volumes/software/tmp";
    

    public static List<String> pathName = new ArrayList<String>();

    public static Map<String,String> map = new HashMap<>();

    public static void init() throws IOException {
        FileReader fr=new FileReader("/Volumes/software/map");
        BufferedReader br=new BufferedReader(fr);
        String str="";
        while((str=br.readLine())!=null){
            String[] split = str.split(",");
            if (split.length == 2) {
                map.put(split[0], split[1]);
            }
        }
        fr.close();
        br.close();
    }

    public static void del(String dirPath){
        File file = new File(dirPath);
        file.delete();
    }

    public static void main(String[] args) throws IOException {
        init();
        System.out.println(map.keySet().size());
        iteratorPath(dirPath);
        for (String s : pathName) {
            System.out.println(s);
            if (s.indexOf("_gm") == -1){
                continue;
            }
            try{
                String copyFilePath = s.replace("_gm","");
                del(copyFilePath);
                FileWriter fw=new FileWriter(copyFilePath);
                BufferedWriter bw=new BufferedWriter(fw);

                ///读取文件的东西
                FileReader fr=new FileReader(s);
                BufferedReader br=new BufferedReader(fr);
                String str="";
                while((str=br.readLine())!=null){
                    if (!str.trim().isEmpty()){
                        for (String key : map.keySet()) {
                            if (str.indexOf(key) != -1){
                                str = str.replace(key,map.get(key));
                                System.out.println("替换后："+str);
                            }
                        }
                    }
                    bw.write(str+"\r\n");
                }
                bw.flush();
                fw.close();
                bw.close();
                fr.close();
                br.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void iteratorPath(String dir) {
        File or;
        File[] files;
        or = new File(dir);
        files = or.listFiles();
        if (files != null) {
        for (File file : files) {
            if (file.isFile()) {
                if (file.getPath().indexOf("DS")!=-1){
                    continue;
                }
                pathName.add(file.getPath());
            }else if (file.isDirectory()) {
                iteratorPath(file.getAbsolutePath());
            }
        }
    }
}
}
